<template>
  <div>
    <div class="table-container">
      <el-card class="operate-container" shadow="never">
        <i class="el-icon-tickets"></i>
        <span>数据列表</span>
        <el-button
          v-show="hasPermission('promotion:ad:add')"
          size="mini"
          @click="handleAdd"
          class="btn-add">添加
        </el-button>
      </el-card>
      <div class="table-container">
        <el-table ref="returnReasonTable"
                  :data="list"
                  style="width: 100%;"
                  v-loading="listLoading" border>
          <el-table-column label="编号" width="80" align="center">
            <template slot-scope="scope">{{scope.row.id}}</template>
          </el-table-column>
          <el-table-column align="center" label="广告名称" prop="name"/>
          <el-table-column align="center" label="头像">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 100px" :src="scope.row.url"  :preview-src-list="[ scope.row.url ]"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态" prop="status">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.status ? '启用' : '禁用' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="添加时间" prop="addTime"/>
          <el-table-column align="center" label="修改时间" prop="updateTime"/>
          <el-table-column label="操作" width="280" align="center">
            <template slot-scope="scope">
              <el-button
                v-show="hasPermission('promotion:ad:edit')"
                size="mini"
                @click="handleUpdate(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                v-show="hasPermission('promotion:ad:deleted')"
                size="mini"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,30]"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {fetchList,deleteData} from '@/api/ad';
  import store from '@/store'
  export default {
    name: 'ad',
    components: {},
    data() {
      return {
        list:[],
        listLoading:true,
        total:null,
        listQuery: {
          pageNum: 1,
          pageSize: 5
        },
      }
    },
    created() {
      this.getList();
    },
    filters: {
    },
    methods: {
      handleAdd() {
        this.$router.push({
          path: '/promotion/adDetail'
        })
      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        }).catch(reason => {})
      },
      handleDelete(index, row) {
        this.$confirm('是否要进行该删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteData(row.id).then(response => {
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1000
            });
            this.getList();
          }).catch(reason => {});
        })
      },
      handleUpdate(index, row) {
        this.$router.push({
          path: '/promotion/adDetail',
          query: {
            id: row.id
          }
        })
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      hasPermission(perms){
        return store.getters.roles.some(role => perms === role)
      }
    }
  }
</script>
<style>
</style>

